/**
 * 
 */
package es.ubu.ceudaaj.exercise.interfaz;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JButton;

import es.ubu.ceudaaj.exercise.ud02.t04.l03.Agenda;
import es.ubu.ceudaaj.exercise.ud02.t04.l03.AgendaJAXB;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFormattedTextField;

/**
 * 
 * @copirygth
 * @author Miguel
 * @version
 * 
 */
public class EliminarJAXB extends JFrame {
	private Agenda agenda;
	private JPanel contentPane;
	private JLabel lblEliminar;
	private JButton btnAceptar;
	private JButton btnCancelar;
	private Integer num;
	private JTextField tfNum;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EliminarJAXB frame = new EliminarJAXB();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EliminarJAXB() {
		agenda = AgendaJAXB.getInstance();
		setTitle("Eliminar Contacto");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0 };
		gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_contentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 1.0,
				1.0, Double.MIN_VALUE };
		gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, Double.MIN_VALUE };
		contentPane.setLayout(gbl_contentPane);

		lblEliminar = new JLabel("Seleccione el n\u00BA de Contacto:");
		GridBagConstraints gbc_lblEliminar = new GridBagConstraints();
		gbc_lblEliminar.insets = new Insets(0, 0, 5, 5);
		gbc_lblEliminar.gridx = 2;
		gbc_lblEliminar.gridy = 2;
		contentPane.add(lblEliminar, gbc_lblEliminar);

		btnAceptar = new JButton("Aceptar");
		btnAceptar.addMouseListener(new BtnAceptarMouseListener());

		tfNum = new JTextField();
		GridBagConstraints gbc_tfNum = new GridBagConstraints();
		gbc_tfNum.insets = new Insets(0, 0, 5, 5);
		gbc_tfNum.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfNum.gridx = 4;
		gbc_tfNum.gridy = 2;
		contentPane.add(tfNum, gbc_tfNum);
		tfNum.setColumns(10);
		GridBagConstraints gbc_btnAceptar = new GridBagConstraints();
		gbc_btnAceptar.insets = new Insets(0, 0, 0, 5);
		gbc_btnAceptar.gridx = 2;
		gbc_btnAceptar.gridy = 6;
		contentPane.add(btnAceptar, gbc_btnAceptar);

		btnCancelar = new JButton("Cancelar");
		btnCancelar.addMouseListener(new BtnCancelarMouseListener());
		GridBagConstraints gbc_btnCancelar = new GridBagConstraints();
		gbc_btnCancelar.insets = new Insets(0, 0, 0, 5);
		gbc_btnCancelar.gridx = 4;
		gbc_btnCancelar.gridy = 6;
		contentPane.add(btnCancelar, gbc_btnCancelar);
	}

	private class BtnCancelarMouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent arg0) {
			dispose();
		}
	}

	private class BtnAceptarMouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent arg0) {

			num = new Integer(tfNum.getText());
			agenda.deleteContact(num);
			JOptionPane.showMessageDialog(EliminarJAXB.this,
					"El contacto se elimino correctamente");
			dispose();

			/*
			 * else{ JOptionPane.showMessageDialog(EliminarJAXB.this,
			 * "El contacto no se pudo eliminar"); dispose(); }
			 */
		}
	}
}
